package com.ywgt.controller;

import com.github.pagehelper.Page;
import com.ywgt.pojo.Coupon;
import com.ywgt.pojo.CouponUser;
import com.ywgt.service.CouponService;
import com.ywgt.service.CouponUserService;
import com.ywgt.util.ResponseUtil;
import io.swagger.annotations.ApiModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/admin/coupon")
@ApiModel("优惠卷管理")
public class CouponController {

    @Autowired
    CouponService couponService;

    @Autowired
    CouponUserService couponUserService;

    /**
     *  page: 1,
     *         limit: 20,
     *         name: undefined,
     *         type: undefined,
     *         status: undefined,
     *         sort: 'add_time',
     *         order: 'desc'
     * @return
     */
    @GetMapping("/list")
    public Object getCoupons(String name, Integer type,Integer status, @RequestParam(required = false,defaultValue = "add_time") String sort,
                             @RequestParam(required = false,defaultValue = "desc") String order,
                             @RequestParam(required = false,defaultValue = "1") int page,
                             @RequestParam(required = false,defaultValue = "10") int limit){
        Page<Coupon> coupons=couponService.getCoupons(name,type,status,page,limit,sort,order);
        List<Coupon> items = coupons.getResult();
        long total = coupons.getTotal();
        Map<String,Object> data=new HashMap<>();
        data.put("total",total);
        data.put("items",items);
        return ResponseUtil.ok(data);
    }

    @GetMapping("/read")
    public Object read(Integer id){
        Coupon coupon = couponService.getById(id);
        return ResponseUtil.ok(coupon);
    }

    /**
     * page: 1,
     *         limit: 20,
     *         couponId: 0,
     *         userId: undefined,
     *         status: undefined,
     *         sort: 'add_time',
     *         order: 'desc'
     * @return
     */
    @GetMapping("/listUsers")
    public Object getUsers(Integer userId, Integer couponId,Integer status, @RequestParam(required = false,defaultValue = "add_time") String sort,
                           @RequestParam(required = false,defaultValue = "desc") String order,
                           @RequestParam(required = false,defaultValue = "1") int page,
                           @RequestParam(required = false,defaultValue = "10") int limit){
        Page<CouponUser> couponUsers= couponUserService.getCouponUsees(userId,couponId,status,page,limit,sort,order);
        List<CouponUser> items = couponUsers.getResult();
        long total = couponUsers.getTotal();
        Map<String,Object> data=new HashMap<>();
        data.put("total",total);
        data.put("items",items);
        return ResponseUtil.ok(data);
    }

    @PostMapping("/create")
    public Object create(@RequestBody Coupon coupon){
        if(couponService.save(coupon)){
            return ResponseUtil.ok();
        }
        return ResponseUtil.fail();
    }

    @PostMapping("/delete")
    public Object delete(@RequestBody Coupon coupon){
        if(couponService.removeById(coupon.getId())){
            return ResponseUtil.ok();
        }
        return ResponseUtil.fail();
    }

    @PostMapping("/update")
    public Object update(@RequestBody Coupon coupon){
        if(couponService.updateById(coupon)){
            return ResponseUtil.ok();
        }
        return ResponseUtil.fail();
    }
}
